//
//  Masonry+Utils.m
//  YZStock
//
//  Created by fenchol on 15/8/31.
//  Copyright (c) 2015年 cqjr. All rights reserved.
//

#import "Masonry+Utils.h"

@implementation NSArray (Utils)

- (void)mas_linkInHorizon
{
    [self mas_linkInHorizonWithSpace: 0];
}

- (void)mas_linkInVertical
{
    [self mas_linkInVerticalWithSpace: 0];
}

- (void)mas_linkInHorizonWithSpace: (CGFloat)space
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(preView.mas_trailing).with.offset(space);
        }];
        preView = currentView;
    }
}

- (void)mas_linkInVerticalWithSpace: (CGFloat)space
{
    
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(preView.mas_bottom).with.offset(space);
        }];
    }
}


- (void)mas_queueInHorizonWithSpace: (CGFloat)space
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(preView);
            make.bottom.equalTo(preView);
            make.leading.equalTo(preView.mas_trailing).with.offset(space);
            make.width.equalTo(firstView);
        }];
        preView = currentView;
    }
}

- (void)mas_queueInVerticalWithSpace: (CGFloat)space
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(preView);
            make.trailing.equalTo(preView);
            make.top.equalTo(preView.mas_bottom).with.offset(space);
            make.height.equalTo(firstView);
        }];
        preView = currentView;
    }
}

- (void)mas_queueInHorizon
{
    [self mas_queueInHorizonWithSpace: 0];
}

- (void)mas_queueInVertical
{
    [self mas_queueInVerticalWithSpace: 0];
}

- (void)mas_queueInHorizonWithGroupCount: (NSInteger)gCount innerSpace: (CGFloat)innerSpace groupSpace: (CGFloat)groupSpace
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (NSInteger i = 1; i < self.count; i++) {
        UIView *currentView = self[i];
        CGFloat space = (i % gCount) == 0 ? groupSpace : innerSpace;
        
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(preView);
            make.bottom.equalTo(preView);
            make.leading.equalTo(preView.mas_trailing).with.offset(space);
            make.width.equalTo(firstView);
        }];
        preView = currentView;
    }
}


- (void)mas_queueInVerticalWithGroupCount: (NSInteger)gCount innerSpace: (CGFloat)innerSpace groupSpace: (CGFloat)groupSpace
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (NSInteger i = 1; i < self.count; i++) {
        UIView *currentView = self[i];
        CGFloat space = (i % gCount) == 0 ? groupSpace : innerSpace;
        
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(preView);
            make.trailing.equalTo(preView);
            make.top.equalTo(preView.mas_bottom).with.offset(space);
            make.height.equalTo(firstView);
        }];
        preView = currentView;
    }
}

- (void)mas_equalHorizonAxis
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(preView);
            make.bottom.equalTo(preView);
        }];
        preView = currentView;
    }
}

- (void)mas_equalVerticalAxis
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    UIView *preView = firstView;
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(preView);
            make.trailing.equalTo(preView);
        }];
        preView = currentView;
    }
}

- (void)ns_equalAttribute: (NSLayoutAttribute)attr
{
    if (self.count == 0) {
        return;
    }
    UIView *firstView = self[0];
    for (UIView *currentView in self) {
        if (currentView == firstView) {
            continue;
        }
        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem: currentView
                                                                      attribute: attr
                                                                      relatedBy: NSLayoutRelationEqual
                                                                         toItem: firstView
                                                                      attribute: attr multiplier: 1.0
                                                                       constant: 0];
        [firstView.superview addConstraint: constraint];
    }
}

- (void)mas_hintValueQueueWithInnerSpace: (CGFloat)innerSpace groupSpace: (CGFloat)groupSpace
{
    if (self.count < 2) {
        return;
    }
    UIView *firstHintView = self[0];
    UIView *fisstValueView = self[1];
    UIView *preView = firstHintView;
    for (NSInteger i = 1; i < self.count; i++) {
        UIView *currentView = self[i];
        CGFloat space = (i % 2) == 0 ? groupSpace : innerSpace;
        
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(firstHintView);
            make.bottom.equalTo(firstHintView);
            make.leading.equalTo(preView.mas_trailing).with.offset(space);
            if (i%2 == 0) {
                make.width.equalTo(firstHintView);
            }
            else
            {
                make.width.equalTo(fisstValueView);
            }
        }];
        preView = currentView;
    }
}
@end
